$(function(){
	showLoginInfo();
	formOpera();
})

function showLoginInfo(){
	$.ajax({
		type : 'get',
    	url : access_url+'/index/homepage/getlogininfofromcache',
    	dataType : 'json',
    	success : function(data){
    		if(data.code != 200){
				layer.msg(data.msg, {icon: 5});
				return;
			}
    		var loginInfo = data.retdata;
    		
    		if(isNull(loginInfo)){
    			layer.msg('未获取到登陆人信息!', {icon: 5});
    		}
    		
    		console.log("loginInfo",loginInfo);
    		
    		var login_id = loginInfo.login_id;
    		var orgid = loginInfo.orgid;
    		
    		$('#login_id').val(login_id);
    		
    		var organization = loginInfo.organization;
    		
    		if(!isNull(organization)){
    			var orgname = organization.orgname;
    			$('#orgid').val(orgname);
    			$('#orgid').attr('data-value',orgid);
    		}
    		
    		var person = loginInfo.person;
    		
    		if(!isNull(person)){
    			var person_name = person.person_name;
    			var phone_no = person.phone_no;
    			var qqno = person.qqno;
    			var wechatid = person.wechatid;
    			var email = person.email;
    			var birth = person.birth;
    			var addr = person.addr;
    			
    			$('#person_name').val(person_name);
    			$('#phone_no').val(phone_no);
    			$('#qqno').val(qqno);
    			$('#wechatid').val(wechatid);
    			$('#email').val(email);
    			$('#birth').val(birth);
    			$('#addr').val(addr);
    		}
    	}
	})
}

var formOpera = function(){
	layui.use(['form','layer','laydate'],function(){
		var form = layui.form
			,layer = layui.layer
			,laydate = layui.laydate;
		
		laydate.render({
			elem: '#birth'
			,trigger: 'click'
		});
		
		$('#orgid').click(function(){
			layer.open({
				type : 2
				,title : '选择组织机构'
				,area : ['300px','450px']
				,offset : '20px'
				,fixed : false
				,content : 'chooseOrg.html'
				,resize : false
			})
		})
	})
}

//点击修改密码
$('#click-show').click(function(){
	var isShow = $(this).attr('isShow');
	
	if('0'==isShow){
		$(this).text('放弃修改');
		$(this).attr('isShow','1');
		$('#update-passwd-div').slideDown();
		$('#sampleTips').html('');
	} else {
		$(this).text('修改密码');
		$(this).attr('isShow','0');
		$('#update-passwd-div').slideUp();
		$('#old_passwd').val('');
		$('#new_passwd').val('');
		$('#new_passwd_again').val('');
		$('#sampleTips').html('');
	}
})

//更新个人信息
$('#update-person-info').click(function(){
	var login_id = $('#login_id').val();
	var orgid = $('#orgid').attr('data-value');
	var old_passwd = $('#old_passwd').val();
	var new_passwd = $('#new_passwd').val();
	var new_passwd_again = $('#new_passwd_again').val();
	var person_name = $('#person_name').val();
	var phone_no = $('#phone_no').val();
	var email = $('#email').val();
	var qqno = $('#qqno').val();
	var wechatid = $('#wechatid').val();
	var birth = $('#birth').val();
	var addr = $('#addr').val();
	
	if(isNull(orgid)){
		layer.msg('未选择组织机构!', {icon: 5});
		return;
	}
	
	if(isNull(phone_no)){
		layer.msg('未填写联系方式!', {icon: 5});
		return;
	}
	
	if(!isNull(new_passwd)&&isNull(old_passwd)){
		layer.msg('未输入原密码', {icon: 5});
		return;
	}
	
	if(!isNull(new_passwd)&&!pwdSame){
		layer.msg('两次输入的密码不一致!', {icon: 5});
		return;
	}
	
	$.ajax({
		type : 'post',
    	url : access_url+'/system/systemmanage/updatepersoninfo',
    	contentType : 'application/json;charset=UTF-8',
    	data : JSON.stringify({
    		login_id : login_id
			,login_password : new_passwd
			,old_password : old_passwd
			,orgid : orgid
			,person : {
				person_id : login_id
				,person_name : person_name
				,phone_no : phone_no
				,email : email
				,qqno : qqno
				,wechatid : wechatid
				,addr : addr
				,birth : birth
			}
		}),
    	dataType : 'json',
    	success : function(data){
    		if(data.code != 200){
				layer.msg(data.msg, {icon: 5});
				return;
			}
    		layer.msg(data.msg);
    		parent.location.reload(true);
    	}
	})
})

var pwdSame = true;
$('#new_passwd_again').blur(function(){
	var new_passwd = $('#new_passwd').val();
	var new_passwd_again = $('#new_passwd_again').val();
	
	if(new_passwd != new_passwd_again){
		layer.tips('两次输入的密码不一致','#new_passwd_again',{
			tips : [2,'red']
		});
		pwdSame = false;
	} else {
		pwdSame = true;
	}
})